package com.sinoglobal.ningxia.fragment;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ab.view.listener.AbOnListViewListener;
import com.ab.view.pullview.AbPullListView;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.IBase;
import com.sinoglobal.ningxia.activity.shop.ShoppingScoreActivity;
import com.sinoglobal.ningxia.adapter.ActsAdapter;
import com.sinoglobal.ningxia.beans.Act_list;
import com.sinoglobal.ningxia.beans.EnentsListVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.thread.ItktAsyncTask;
import com.sinoglobal.ningxia.utils.ItktLog;

/**
 * 
 * @author zbl
 * @createdate 2014-6-23 下午4:57:25
 * @Description:首页活动的信息
 */
public class EnentsInfoFragment extends Fragment implements IBase, AbOnListViewListener {
	private static Context context;
	private View view;
	public static final String TYPE = "type";
	public static final String KEY = "key";
	private int num;// 当前访问数据的类型 typelist（position）
	// private String key = "";//
	private AbPullListView mListView;//
	private TextView tv_noInfor;// 暂无信息
	private ArrayList<Act_list> list;// 测试数据
	private LinearLayout linearLayout1;// 显示正在收索数据
	private ActsAdapter eAdapter;// 内容的适配器
	// private int position;
	public static final String ENENTS = "enent_id";
	private boolean flag = true;
	private boolean flag_0 = true;
	private int pageNum = 0;//
	private int lenghNum = 5;//

	public static EnentsInfoFragment newInstance(Context context, int num, String key) {
		EnentsInfoFragment.context = context;
		EnentsInfoFragment fragment = new EnentsInfoFragment();
		Bundle args = new Bundle();
		args.putInt(TYPE, num);
		args.putString(KEY, key);
		fragment.setArguments(args);
		return fragment;

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		num = getArguments().getInt(TYPE);
		// key = getArguments().getString(KEY);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = LayoutInflater.from(context).inflate(R.layout.activities_listview, null);
		linearLayout1 = (LinearLayout) view.findViewById(R.id.linearLayout1);
		init();
		showListener();
		onRefresh();
		return view;
	}

	@Override
	public void init() {
		mListView = (AbPullListView) view.findViewById(R.id.listView1);
		tv_noInfor = (TextView) view.findViewById(R.id.textView1);
		mListView.setPullLoadEnable(true);
		// eAdapter=new ActsAdapter(context);
		mListView.setAdapter(eAdapter);
		mListView.setAbOnListViewListener(this);

	}

	// 获取数据
	private void getDate(final int pageNum, final int type) {
		new ItktAsyncTask<Void, Void, EnentsListVo>() {
			@Override
			public EnentsListVo before(Void... params) throws Exception {
				// String userid, String limit1 , String limit2,String type
				return RemoteImpl.getInstance().getEnentsListyVo("1", 1);
			}

			@Override
			public void after(EnentsListVo result) {
				ItktLog.e("-------after----------Enents_list_Vo----------------->>" + result);
				if (result != null) {
					linearLayout1.setVisibility(View.GONE);
					if (0 == result.getCode()) {
						mListView.setVisibility(View.VISIBLE);
						if (type == 1) {
							// eAdapter.getEnents_data().clear();
							onLoad();
							flag_0 = true;
						} else {
							onLoad();
							flag = true;
						}
						// list = result.getEnentsDataVos();// 从服务器拿到的数据
						list = result.getAct_list();
						// eAdapter.setResult(list);// 在adapter里面加入新的数据
						eAdapter.notifyDataSetChanged();
					} else {
						if (pageNum == 0) {
							mListView.setVisibility(View.GONE);
							tv_noInfor.setVisibility(View.VISIBLE);
						}
						onLoad();
						flag_0 = true;
						flag = true;
					}
				}
				// if (eAdapter.getEnents_data().size() <= 1) {
				// mListView.setPullLoadEnable(false);
				// }
			}

			@Override
			public void exception() {

			}

		}.execute();

	}

	@Override
	public void onRefresh() {
		if (flag_0) {
			pageNum = 0;
			flag_0 = false;
			if (num == 0) {
				getDate(pageNum, 3);
			} else if (num == 1) {
				getDate(pageNum, 1);
			} else if (num == 2) {
				getDate(pageNum, 2);
			} else if (num == 3) {
				getDate(pageNum, 4);
			}
			eAdapter.notifyDataSetChanged();
		}

	}

	@Override
	public void onLoadMore() {
		if (flag) {
			flag = false;
			pageNum += lenghNum;
			if (num == 0) {
				getDate(pageNum, 3);
			} else if (num == 1) {
				getDate(pageNum, 1);
			} else {
				getDate(pageNum, 2);
			}
		}
	}

	@Override
	public void showListener() {
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				ItktLog.e("嘻嘻哈哈"+ "weizhi" + position);
				// EnentsInfoFragment.this.position = position;
				Intent intent = new Intent(context, ShoppingScoreActivity.class);
				Bundle b = new Bundle();
				b.putSerializable("goods", list.get(position));
				intent.putExtras(b);
				startActivity(intent);
			}
		});

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == 2) {
			Intent intent = new Intent(context, ShoppingScoreActivity.class);
			// 传递点击的ID
			startActivity(intent);
		}
	}

	private void onLoad() {
		mListView.stopRefresh();
		mListView.stopLoadMore();
	}

}
